from sklearn.model_selection import KFold
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import numpy as np

# 示例数据
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])

# 初始化5折交叉验证
kf = KFold(n_splits=5, shuffle=True, random_state=42)
model = LinearRegression()
scores = []

for train_index, test_index in kf.split(X):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    scores.append(mean_squared_error(y_test, y_pred))

print("MSE均值:", np.mean(scores))
print("MSE标准差:", np.std(scores))
